<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * date: 2021-02-24 18:14:56
 * @LastEditors: Sam
 * @LastEditTime: 2021-12-14 18:22:15
 */
namespace app\__MODULE__\model;

class Exchange extends BaseModel {
    
    /**
     * 数据表主键
     * @var string
     */

    protected $pk = 'id';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    
     /**
     * 模型名称
     * @var string
     */
    protected $name = 'exchange';


    public function user(){
        return $this->belongsTo(User::class,'uid')->bind(['nickname']);;
    }

    public function goods(){
        return $this->belongsTo(Product::class,'goods_id')->bind(['goods_name'=>'name','goods_thumb'=>'thumb']);
    }
    /**
     * 异步获取当前列表 信息
     * @param $where
     * @return array
     */
    public static function getList($where)
    {
        $data=self::setWhere($where)->page((int)$where['page'],(int)$where['limit'])->order('create_time desc')->append(['status_text'])->select();
        $data=count($data) ? $data->toArray() : [];
        $count=self::setWhere($where)->count();
        return compact('data','count');
    }

    /**
     * 获取查询条件
     * @param $where
     * @param string $alert
     * @param null $model
     * @return SystemUserLevel|null
     */
    public static function setWhere($where,$alert='',$model=null)
    {
        $model=$model===null ? self::with(['user','goods']) : $model;
        if(isset($where['user']) && $where['user']!=''){
            $ids = User::where('nickname|mobile','like',"%{$where['user']}%")->column('id');
            if(count($ids)){
                $model = $model->where('uid','in',$ids);
            }
        } 
        if(isset($where['goods']) && $where['goods']!=''){
            $ids = Goods::where('name','like',"%{$where['goods']}%")->column('id');
            if(count($ids)){
                $model = $model->where('goods_id','in',$ids);
            }
        } 
        if(isset($where['status']) && $where['status'] != '') {
            $model = $model->where('status',$where['status']);
        }
        return $model;
    }

    /**
     * 类型转换
     * @param [type] $value
     * @return void
     * @author Sam
     * date 2021-02-25
     */
    public function getTypeAttr($value){
        $status = [2=>'虚拟',1=>'实物'];
        return $status[$value];
    }

    public function getStatusTextAttr($value,$data){
        $status = [0=>'待发货',1=>'已发货',2=>'已收货'];
        return $status[$data['status']];
    }

       /**
     * 积分收入
     * @param  [int]  $uid         [用户id]
     * @param  [double]  $score       [积分]
     * @param  [int]  $type        [类型 [1=> '注册',2=> '签到', 3=>'商城退还'];]
     * @param  integer $realtion_id [关联id]
     * @return [type]               [description]
     */
    public static function in_score($uid,$score,$type,$realtion_id = 0){
        $user = User::get($uid);
        $user->score += abs($score);
        $user->update_score_time= time();
        $user->save();
        ScoreLog::create([
            'uid' => $uid,
            'score' => $score,
            'mark' => 1,
            'in_type' => $type,
            'relation_id' => $realtion_id
        ]);
        return true;    
    }

          /**
     * 积分支出
     * @param  [int]  $uid         [用户id]
     * @param  [double]  $score       [积分]
     * @param  [int]  $type        [类型 [1=> '商城消费']
     * @param  integer $realtion_id [关联id]
     * @return [type]               [description]
     */
    public static function out_score($uid,$score,$type,$realtion_id = 0){
        $user = User::get($uid);
        $user->score -= abs($score);
        $user->update_score_time= time();
        $user->save();
        ScoreLog::create([
            'uid' => $uid,
            'score' => $score,
            'mark' => -1,
            'out_type' => $type,
            'relation_id' => $realtion_id
        ]);
        return true;    
    }
}

